﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace Elementum
{
    public class FileIo
    {
        public static void Save(DataNeedSave toSave)
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(DataNeedSave));

            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();   // grab the storage
            FileStream stream = store.OpenFile("123456.save", FileMode.Create);              // Open a file in Create mode
            serializer.WriteObject(stream, toSave);
            stream.Close();
        }
        public static DataNeedSave Load()
        {
            //return new DataNeedSave();
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            DataContractSerializer serializer = new DataContractSerializer(typeof(DataNeedSave));
            if (store.FileExists("123456.save")) // Check if file exists
            {
                IsolatedStorageFileStream save = new IsolatedStorageFileStream("123456.save", FileMode.Open, store);

                DataNeedSave read = (DataNeedSave)serializer.ReadObject(save);
                //read.StateStateTable
                return read;
            }
            else
            {
                return new DataNeedSave();
            }
        }

    }
}
